Professional Documents
Culture Documents
com
A service for building, sharing and publishing collections of
videos
● Backend logic
– session management
● should be easier with RFacebook 0.6 (we use 0.5.1)
– app installation/removal
– linking facebook accounts to vod:pod user accounts
● Display logic
– Canvas Page (iframe or fbml)
– Facebook profile box
● News Feed
– post activity to a users news feed
Facebook on Rails
● Iframe
– Requests to the canvas page are redirected
to your app server, and displayed in an
iframe
– Pros
● Full html, css, javascript support
● Easy to port existing rails views
– Cons
● You need to write CSS to mimic facebook UI
● Handy FBML extensions like 'fb:friend-selector'
need to be written by hand
Setting up your App
● http://www.facebook.com/developers
– apply for new key
● Canvas Page URL
– Path on facebook where your app will live
– http://apps.facebook.com/vodpod-videos/
● Callback URL
– Path to the facebook app on your servers
– http://facebook.vodpod.com/facebook/
– Requests to the canvas URL will be redirected to the
callback URL
– apps.facebook.com/vodpod-videos/user/spencer ->
facebook.vodpod.com/facebook/user/spencer
RFacebook
if params[:auth_token]
begin
session[:facebook_session].activate_with_token(params[:auth_token])
rescue RFacebook::FacebookSession::RemoteException => e
redirect_to session[:facebook_session].get_login_url
return false
end
end
end
Session Logic/Workflow,
Part 2
● User loads your canvas page, is already authenticated
and has session
– Vod:Pod looks up user with the FB uid.
– If user exists, sets as the current_user.
before_filter :fb_check_user
def fb_check_user
# Is the facebook session authenticated?
if @fb_session.session_uid
# lookup vodpod user
user = User.find_by_facebook_uid(@fb_session.session_uid)
if user
self.current_user = user
return
end
end
self.current_user = nil
session[:user] = nil
end
REST API
(response/'user').each do |user|
uid = user.at('uid').inner_html
name = user.at('name').inner_html
pic_url = user.at('pic_square').inner_html
def get_uninvited_friends(uid)
response = fbsession.fql_query(
{:query => "SELECT uid, name, pic_square FROM user WHERE \
uid IN (SELECT uid2 FROM friend WHERE uid1=#{uid}) AND NOT is_app_user"})
● See: http://www.livelearncode.com/archives/14
class FacebookController < ApplicationController
before_filter :require_facebook_login, :only => [:index]
@firstName = attrs[:first_name]
@lastName = attrs[:last_name]
end
private
attrs = {}
attributes.each {|key| attrs[key.to_sym] =
response.search(key.to_s)[0].inner_html}
attrs
end
end
Creating the Profile FBML
Block
Creating the Profile FBML
Block
● Call profile.setFBML, with the user_id to update, and the FBML.
● You need to do this whenever you want to update the profile
fbsession.profile_setFBML({:uid => fbsession.session_uid,
:markup => render_to_string(:partial => 'profile_fbml',
:locals => {:user => current_user})
})
The FBML:
<fb:title>Vod:Pod Videos</fb:title>
<fb:subtitle>
<fb:name uid="profileowner" firstnameonly="true" useyou="false"/> has
collected <%= pluralize(user.facebook_pod.videos_count, 'video') %>.
</fb:subtitle>
<div style="margin-left:30px;">
<% videos.each do |video| %>
<%= link_to image_tag(video.thumbnail_url), video_url(video) %>
<% end %>
</div>
Invite Friends to use App
def invite
if request.post?
begin
response = fbsession.notifications_sendRequest(
{:to_ids => params[:facebook_uids],
:type => 'Vod:Pod',
:content => “FBML for the request message”,
:image => 'http://www.vodpod.com/images/vodpod_square.gif',
:invite => false})
rescue Exception => e
return render :text => “error”, :content_type => 'text/plain'
end
def post_feed_item(title)
begin
fbsession.feed_publishActionOfUser({:uid =>
fbsession.session_uid, :title => title})
rescue Exception => e
log "FACEBOOK: Couldn't post comment to news feed: #{e}"
end
end
● http://developers.facebook.com
– API docs, FBML docs, API test console (crappy and limited),
FBML test console, Wiki
● http://www.facebook.com/developers
– Completely different than developers.facebook.com – GREAT!
– Discussion forum
– Create/administer your applications here
● http://www.livelearncode.com/archives/14
– Rfacebook documentation
● http://geekblog.vodpod.com
– This presentation